var app = getApp();
Page({
    data: {
        cellphone: '',
        verifiedCode: ''
    },
    onLoad: function onLoad() {        
    },
    textinput: function textinput(e) {
        var type = e.currentTarget.dataset.type;
        if (type == 1) {
            this.setData({
                cellphone: e.detail.value
            });
        }
        if (type == 2) {
            this.setData({
                verifiedCode: e.detail.value
            });
        }
    },
    sendcode: function sendcode() {
        wx.request({
            url: 'http://localhost:8080/wxAppletContact/sendVerifiedCode',
            data: {
                cellphone: this.data.cellphone,
                operation: "Login"
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function success(res) {
                if (res.data.statusCode == 200) {
                    wx.showToast({
                        title: '验证码发送成功',
                        icon: 'success',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 2000);
                }
            },
            fail: function fail(res) {
                wx.showToast({
                    title: '验证码发送失败',
                    icon: 'loading',
                    duration: 2000
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 2000);
            },
            complete: function complete(res) {
                console.log(res);
            }
        });
    },
    login: function login(e) {
        var that = this;        
        wx.request({
            url: 'http://localhost:8080/wxAppletContact/login',
            data: {
                cellphone: that.data.cellphone,
                verifiedCode: that.data.verifiedCode,
                openId: app.globalData.openId
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },            
            complete: function(res) {
                if(res.statusCode == 200) {
                    wx.navigateTo({
                        url: '../contact'
                    });
                } else {
                    console.log(res.data.errorMessage)
                }
            }
        });
    }
});